class Solution {
  public:
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     * max water
     * @param arr int整型vector the array
     * @return long长整型
     */
    long long maxWater(vector<int>& arr) {
        int len = arr.size();
        if (len <= 2) return 0;
        long long sum = 0;
        vector<int> left(len, 0);
        vector<int> right(len, 0);
        left[0] = arr[0];
        right[len - 1] = arr[len - 1];
        for (int i = 1; i < len; i++) left[i] = max(left[i - 1], arr[i]);
        for (int i = len - 2; i >= 0; i--) right[i] = max(right[i + 1], arr[i]);
        for (int i = 1; i < len - 1; i++) {
            sum += min(left[i], right[i]) - arr[i];
        }
        return sum;
    }
};